#include "WhiteBoardManager.h"
#include "Manager.h"
#include "common.h"

CWhiteBoardManager::CWhiteBoardManager(CManager *pMan)
: m_pWhiteBoard(NULL)
, m_pMan(pMan)
, m_bExit(false)
, m_bSync(false)
, m_bLocalWb(false)
{
	m_pFileBuff = NULL;
}

CWhiteBoardManager::~CWhiteBoardManager(void)
{
	m_bExit = true;

	WaitForStop();

	if (m_pWhiteBoard)
	{
		ITC_DestoryWhiteboard(m_pWhiteBoard);
		m_pWhiteBoard = NULL;
	}

	while(m_lstSendFile.size())
	{
		PhotoInfo *pInfo = NULL;

		{
			CAutoLock autoLock(&m_lockSendFile);
			pInfo = (PhotoInfo *)m_lstSendFile.front();
			m_lstSendFile.pop_front();
		}

		if (pInfo)
		{
			delete pInfo;
			pInfo = NULL;
		}
	}

	if (m_pFileBuff)
	{
		delete [] m_pFileBuff;
		m_pFileBuff = NULL;
	}
}

bool CWhiteBoardManager::InitWhiteBoard(string strFilePath)
{
	if (m_pWhiteBoard)
	{
		ITC_DestoryWhiteboard(m_pWhiteBoard);
		m_pWhiteBoard = NULL;
	}

	m_pWhiteBoard = ITC_CreateWhiteboard();
	if (!m_pWhiteBoard)
		return false;

	m_strPath = strFilePath;

	m_pWhiteBoard->SetVirtualPrintPath((char *)strFilePath.c_str());
	m_pWhiteBoard->SetMemberId(m_pMan->GetTerminalId());
	m_pWhiteBoard->SetWBCallback(this);
	m_pWhiteBoard->EnableFileTransfer(1);

	return StartThread();
}

uint32_t CWhiteBoardManager::GetWindowHandle(void)
{
	return m_pWhiteBoard->GetWindowHandle();
}

void CWhiteBoardManager::EnableDataOperationRight(bool bEnable)
{
	m_pWhiteBoard->EnableDataOperationRight(bEnable);
}

void CWhiteBoardManager::EnadleSync(bool bEnable)
{
	m_bSync = bEnable;
}

void CWhiteBoardManager::HandleRecvWBData(const char *pData, int nDataLen)
{
	if (!m_bLocalWb)
		m_pWhiteBoard->ReceivedWBMessage((uint8_t *)pData, nDataLen - 1);
}

void CWhiteBoardManager::CompleteFileReceivingTask(void)
{
	if (!m_bLocalWb)
		m_pWhiteBoard->CompleteFileReceivingTask(1);
}

void CWhiteBoardManager::SetLocalMode(bool bLocal)
{	
	m_bLocalWb = bLocal;
}

void CWhiteBoardManager::ClearLocalWbData(void)
{
	m_pWhiteBoard->CleanDrawBroad();
}

void CWhiteBoardManager::ThreadProcMain(void)
{
	while (!m_bExit)
	{
		while(m_lstSendFile.size())
		{
			PhotoInfo *pInfo = NULL;

			{
				CAutoLock autoLock(&m_lockSendFile);
				pInfo = (PhotoInfo *)m_lstSendFile.front();
				m_lstSendFile.pop_front();
			}

			if (pInfo)
			{
				HandleSendFile((void *)pInfo);

				delete pInfo;
				pInfo = NULL;
			}			
		}

		my_sleep(10);
	}
}

void CWhiteBoardManager::OnSendWBFile(char* fileName, uint32_t fileId)
{
	if (m_bLocalWb)
		return;

	PhotoInfo *pInfo = new PhotoInfo;
	if (pInfo)
	{
		pInfo->nTerminalId = m_pMan->GetTerminalId();
		memcpy(pInfo->szFileName, fileName, strlen(fileName));

		{
			CAutoLock autoLock(&m_lockSendFile);
			m_lstSendFile.push_back(pInfo);
		}		
	}
}

void CWhiteBoardManager::OnSendWBMessage(uint8_t* buffer, uint32_t size, uint32_t destId /*= 0*/)
{
	if (!m_bLocalWb)
		m_pMan->SendDataByMediaChannel(WHITEBROAD_DATA, (const char *)buffer, size);
}

void CWhiteBoardManager::OnSendWBViewScrollPos(uint32_t pos1, uint32_t pos2)
{
}

void CWhiteBoardManager::OnCancelFileSendingTask(uint32_t fileId)
{
}

bool CWhiteBoardManager::CanSendWbSyncMessage()
{
	return m_bSync;
}

bool CWhiteBoardManager::OnControlButton(uint32_t controlType)
{
	RecvDataInfo *pRecvInfo = new RecvDataInfo;
	pRecvInfo->nCmd = CMD_WHITEBOARD_MSG;

	m_pMan->PutRecvData(pRecvInfo);

	return true;
}

void CWhiteBoardManager::HandleSendFile(void *pParam)
{
	int nSendLen = 0;
	int nSendNum = 100 * 1024;

	PhotoInfo *pInfo = (PhotoInfo *)pParam;
	if (pInfo)
	{
		if (!InitFileHanlde(pInfo->szFileName, pInfo->nTerminalId))
			return ;

		if (SendFileStart((const char *)pInfo, sizeof(PhotoInfo)) == sizeof(PhotoInfo))
		{
			while (nSendLen < m_nFileLen)
			{
				int nNoSendLen = m_nFileLen - nSendLen;
				if (nNoSendLen > nSendNum)
				{
					int nRet = SendFileData(m_pFileBuff + nSendLen, nSendNum);
					nSendLen += nRet;
				}
				else
				{
					int nRet = SendFileData(m_pFileBuff + nSendLen, nNoSendLen);
					nSendLen += nRet;
				}

				if (nSendLen == m_nFileLen)
				{
					SendFileEnd();
					break;
				}
				else
				{
					my_sleep(10);
				}
			}
		}
	}
}

bool CWhiteBoardManager::InitFileHanlde(const char *pFileName, int nTerminalId)
{
	FILE *pFile = NULL;
	bool bRet = false;

	char szFilePath[256] = {0};
	sprintf_s(szFilePath, "%s\\%d\\%s", m_strPath.c_str(),nTerminalId, pFileName);
	int nRet = fopen_s(&pFile, szFilePath, "rb");
	if (nRet == 0)
	{
		fseek(pFile, 0, SEEK_END);
		m_nFileLen = ftell(pFile);
		fseek(pFile, 0, SEEK_SET);

		if (m_pFileBuff)
		{
			delete [] m_pFileBuff;
			m_pFileBuff = NULL;
		}

		m_pFileBuff = new char[m_nFileLen];
		if (fread((char*)m_pFileBuff, 1, m_nFileLen, pFile) == m_nFileLen)
			bRet = true;

		fclose(pFile);
		pFile = NULL;
	}

	return bRet;
}

int CWhiteBoardManager::SendFileStart(const char *pData, int nDataLen)
{
	return m_pMan->SendDataByMediaChannel(WHITEBROAD_FILE_START, pData, nDataLen);
}

int CWhiteBoardManager::SendFileData(const char *pData, int nDataLen)
{
	return m_pMan->SendDataByMediaChannel(WHITEBROAD_FILE, pData, nDataLen);
}

int CWhiteBoardManager::SendFileEnd(void)
{
	return m_pMan->SendDataByMediaChannel(WHITEBROAD_FILE_END, NULL, 0);
}
